﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WuLiu.Spider.BLL;
using System.Text.RegularExpressions;
using WuLiu.Utility;
using System.Net;

namespace WuLiu.Spider.WindowsApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void StartParse()
        {
            if (string.IsNullOrEmpty(txtHtmlTag.Text))
            {
                return;
            }
            MatchHtmlTag matchHtml = new MatchHtmlTag();
            matchHtml.HtmlTag = txtHtmlTag.Text.Trim();
            if (!string.IsNullOrEmpty(txtTagAttr.Text))
            {
                foreach (var item in txtTagAttr.Text.Split(','))
                {
                    if (string.IsNullOrEmpty(item))
                        continue;
                    MatchTagAttribute tagAttr = new MatchTagAttribute();
                    tagAttr.Name = item.Split('=')[0];
                    tagAttr.Value = item.Split('=')[1];
                    matchHtml.TagAttributeList.Add(tagAttr);
                }
            }
            //MessageBox.Show(WuLiu.Utility.SerializerUtility.SerializeToXmlString(matchHtml));
            //matchHtml.HtmlTagAttributeList.Add("class", "logoRig01");
            var startIndex = -1;
            var endIndex = 0;
            var tmplIndex = 0;
            var isFistMatch = false;
            var matchedNum = 0;
            var matchedAttrNum = 0;
            ParseHTML parse = new ParseHTML();
            parse.Source = this.richTextBox1.Text;
            while (!parse.Eof())
            {
                tmplIndex = parse.CurrentIndex;
                char ch = parse.Parse();
                if (ch == 0)
                {
                    if (parse.m_tag == matchHtml.HtmlTag)
                    {
                        #region
                        matchedAttrNum = 0;
                        foreach (MatchTagAttribute item in matchHtml.TagAttributeList)
                        {
                            HtmlAttribute a = parse.GetTag()[item.Name];
                            if (a != null && a.Value.ToLower() == item.Value.ToLower())
                            {
                                matchedAttrNum++;
                            }
                        }
                        if (matchedAttrNum == matchHtml.TagAttributeList.Count)
                        {
                            startIndex = tmplIndex;
                            isFistMatch = true;
                            matchedNum++;
                        }
                        #endregion
                    }

                    if (startIndex > -1 && !isFistMatch)
                    {
                        if (parse.m_tag == matchHtml.HtmlTag)
                        {
                            matchedNum++;
                        }

                        if (parse.m_tag == ("/" + matchHtml.HtmlTag))
                        {
                            matchedNum--;
                        }

                        if (matchedNum == 0)
                        {
                            endIndex = parse.CurrentIndex;
                            GetMatchText(startIndex, endIndex, parse.Source);
                            startIndex = -1;
                            matchedNum = 0;
                            endIndex = 0;
                        }
                    }

                    if (isFistMatch)
                        isFistMatch = false;
                }
            }
        }

        private void GetMatchText(int startIndex, int endIndex, string source)
        {
            StringBuilder sb = new StringBuilder();
            for (; startIndex < endIndex; startIndex++)
            {
                sb.Append(source[startIndex]);
            }

            MessageBox.Show(sb.ToString());
        }

        private void button1_Click(object sender, EventArgs e)
        {
           
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var url = new Uri("http://passport.enntrading.com/LoginForJS.ashx");
            Dictionary<string, string> postData = new Dictionary<string, string>();
            postData.Add("LoginName", "enn_buyer");
            postData.Add("Password", "abc123123");

            //设置标头属性
            RequestSendHeaderAttribute rsha = new RequestSendHeaderAttribute();
            rsha.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
            rsha.ContentType = "application/x-www-form-urlencoded";
            rsha.Method = "Post";
            rsha.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; InfoPath.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";

            CookieCollection cookieCollectoin;
            WebRequestLoginImpersonate webRequestLogin = new WebRequestLoginImpersonate();
            cookieCollectoin = webRequestLogin.GetCookie(url, postData, rsha);

            url = new Uri("Http://trade.enntrading.com/index.aspx");
            var html = webRequestLogin.GetHtmlContent(url, cookieCollectoin, rsha);
        }
    }
}
